﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.UserTypes;
using System.Globalization;
using NHibernate.Type;
using NHibernate.SqlTypes;
using System.Data.Common;

namespace FlexiCommerce.Components
{
    [Serializable]
    public class Culture : PrimitiveType, IConvertible
    {
        public static readonly Culture English = new Culture("en-US");
        public static readonly Culture Spanish = new Culture("es-mx");

        public static Culture FromCultureInfo(CultureInfo culture)
        {
            return new Culture(culture.Name);
        }

        private CultureInfo culture = null;
        public Culture()
            : base(SqlTypeFactory.GetString(256))
        {
            culture = Culture.English.CultureInfo;
        }
        public Culture(string name)
            : base(SqlTypeFactory.GetString(256))
        {
            culture = new CultureInfo(name);  
        }

        public override object DefaultValue
        {
            get { return Culture.English; }
        }

        public override string ObjectToSQLString(object value, NHibernate.Dialect.Dialect dialect)
        {
            return ((Culture)value).CultureInfo.Name.ToString();
        }

        public override Type PrimitiveClass
        {
            get { return typeof(Culture); }
        }

        public override object FromStringValue(string xml)
        {
            return new Culture(xml);
        }

        public override object Get(System.Data.IDataReader rs, string name)
        {
            return FromStringValue(rs[name].ToString());
        }

        public override object Get(System.Data.IDataReader rs, int index)
        {
            return FromStringValue(rs[index].ToString());
        }

        public override void Set(System.Data.IDbCommand cmd, object value, int index)
        {
            Culture type = value as Culture;
            DbParameter param = cmd.Parameters[index] as DbParameter;
            param.Value = type.CultureInfo.Name;

        }

        public override string Name
        {
            get { return culture.Name; }
        }

        public override Type ReturnedClass
        {
            get { return typeof(Culture); }
        }

        public CultureInfo CultureInfo
        {
            get { return culture; }
            set { culture = value; }
        }


        #region IConvertible Members

        public TypeCode GetTypeCode()
        {
            throw new NotImplementedException();
        }

        public bool ToBoolean(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public byte ToByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public char ToChar(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public DateTime ToDateTime(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public decimal ToDecimal(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public double ToDouble(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public short ToInt16(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public int ToInt32(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public long ToInt64(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public sbyte ToSByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public float ToSingle(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public string ToString(IFormatProvider provider)
        {
            if (culture != null)
                return culture.Name.ToString();
            return "";
        }

        public object ToType(Type conversionType, IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public ushort ToUInt16(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public uint ToUInt32(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public ulong ToUInt64(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override bool Equals(object obj)
        {
            Culture culture = obj as Culture;

            if (culture == null)
                return false;

            bool value = culture.CultureInfo.Name.Equals(this.CultureInfo.Name);
            return value;
        }
    }
}
